package com.lhy.tool.patchplugin.strategy;

import com.lhy.tool.patchplugin.PatchPluginProperties;
import com.lhy.tool.patchplugin.model.CompileFileWrapper;
import com.lhy.tool.patchplugin.model.SrcFileWrapper;
import com.lhy.tool.patchplugin.model.TargetFileWrapper;
import com.lhy.tool.patchplugin.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.util.Collections;
import java.util.List;

/**
 * 静态资源文件策略类
 * 配置文件 页面文件等无需编译的静态文件
 */
@Slf4j
public class ResourceStrategyImpl implements FileStrategy {

    @Autowired
    PatchPluginProperties patchPluginProperties;

    protected String srcSubPath;

    protected String targetSubPath;

    public ResourceStrategyImpl(String srcSubPath, String targetSubPath) {
        this.srcSubPath = srcSubPath;
        this.targetSubPath = targetSubPath;
        if(patchPluginProperties == null){
            this.patchPluginProperties = SpringUtil.getBean(PatchPluginProperties.class);
        }
    }

    @Override
    public boolean accept(SrcFileWrapper srcFileWrapper) {
        return srcFileWrapper.getAbsolutePath().startsWith(patchPluginProperties.getSrcPath() + srcSubPath);
    }

    @Override
    public List<CompileFileWrapper> getCompileFiles(SrcFileWrapper srcFileWrapper) {
        CompileFileWrapper fileWrapper = new CompileFileWrapper(srcFileWrapper.getFile());
        return Collections.singletonList(fileWrapper);
    }

    @Override
    public List<TargetFileWrapper> getTargetFiles(CompileFileWrapper compileFileWrapper) {
        String path = compileFileWrapper.getAbsolutePath()
                .replace(patchPluginProperties.getSrcPath() + srcSubPath, patchPluginProperties.getTargetPath() + targetSubPath);
        File targetFile = new File(path);
        TargetFileWrapper fileWrapper = new TargetFileWrapper(targetFile);
        return Collections.singletonList(fileWrapper);
    }


}
